<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" :labelWidth="100" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="jzyBrand"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="rangeTime"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="openAddForm('add')">新增</el-button>
      </template>
    </FilterTable>
    <div style="text-align:center"></div>
    <!--
      <BaseDialog
      :visible.sync="visible1"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <BrandModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog> -->
    <BaseDialog :visible.sync="visible" :title="dialogTitle" width="800" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm ref="addForm" @close="closeAddForm" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getDiscountmodes } from '@/api/repair/repairBusiness/preferentialWay';
import AddForm from './tabs/addForm';

export default {
  name: 'PreferentialWay',
  components: {
    AddForm
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: getDiscountmodes,
      columns: this.createTableColumns()
      // handleType: '',
      // formData: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '优惠模式代码',
          fieldName: 'discountModeCode',
          placeholder: '请输入...',
          maxlength: 8
        },
        {
          type: 'INPUT',
          label: '优惠模式名称',
          fieldName: 'discountModeName',
          placeholder: '请输入...',
          maxlength: 20
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          width: 90,
          dataIndex: 'column-action',
          sorter: true,
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" disabled={props.row.dataSources !== 10451001} onClick={() => this.openAddForm('edit', props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '优惠模式代码',
          dataIndex: 'discountModeCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '优惠模式名称',
          dataIndex: 'discountModeName',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '工时费优惠率',
          dataIndex: 'labourAmountDiscount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '维修材料费优惠率',
          dataIndex: 'repairPartDiscount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '销售材料费优惠率',
          dataIndex: 'salesPartDiscount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '附加费优惠率',
          dataIndex: 'additionalItemsDiscount',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        // {
        //   title: '保险类默认无优惠',
        //   dataIndex: 'noDiscountByInsurance',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number',
        //   render: props => {
        //     return <span>{this.createDictText(props.row.noDiscountByInsurance, '1004')}</span>;
        //   }
        // },
        {
          title: '开始日期',
          dataIndex: 'discountBeginDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '结束日期',
          dataIndex: 'discountEndDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        // {
        //   title: '是否启用优惠时段',
        //   dataIndex: 'isTimeLimited',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.isTimeLimited, '1004')}</span>;
        //   }
        // },
        // {
        //   title: '优惠时段',
        //   dataIndex: 'discountRangeTime',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'date-range',
        //   render: (props, h) => {
        //     return <span>{this.rangeTime(props.row)}</span>;
        //   }
        // },
        {
          title: '状态',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1001'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1001')}</span>;
          }
        }
      ];
    },
    // addHandle() {
    //   this.handleType = 'add';
    //   this.showHandler();
    // },
    // editHandle(row) {
    //   this.handleType = 'edit';
    //   this.formData = row;
    //   this.showHandler();
    // },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // showHandler() {
    //   this.dialogTitle = `${this.conf[this.handleType]}品牌`;
    //   this.visible = true;
    // },
    // closeHandler(val, type) {
    //   this.visible = val;
    //   if (type === 'refresh') {
    //     // 重新加载
    //     this.fetchParams = { ...this.fetchParams };
    //   }
    // },
    openAddForm(status, row) {
      this.dialogTitle = `优惠模式${this.conf[status]}`;
      // console.log(row)
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.addForm.openModel(status, row);
      });
    },
    closeAddForm() {
      this.fetchParams = { ...this.fetchParams };
      this.visible = false;
    },
    rangeTime(row) {
      console.log(row);
      if (row.discountBeginTime && row.discountEndTime) {
        return row.discountBeginTime + '-' + row.discountEndTime;
      } else return null;
    }
  }
};
</script>

<style lang="less" scoped></style>
